package com.eu.ebpm.engine.cmd.pvm;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.deploy.Transfer;
import com.eu.ebpm.engine.entity.history.HisExecution;
import com.eu.ebpm.engine.entity.history.HisTask;
import com.eu.ebpm.engine.entity.runtime.Execution;
import com.eu.ebpm.engine.entity.runtime.TaskEntity;
import com.eu.ebpm.engine.repository.ExecutionRepository;
import com.eu.ebpm.engine.repository.HisTaskRepository;

/**
 * @author 王晓华
 */
@Service
public class StartEventFinishCommand {

	@Autowired
	private HisTaskRepository hisTaskRepository;
	
	@Autowired
	private ExecutionRepository executionRepository;
	
	public void execute(Mission mission, TaskEntity task, Transfer nextTransfer) {
		
		Date currentTime = new Date();
		
		// 1、插入历史任务表
		hisTaskRepository.insert(new HisTask(mission.getType(), mission.getName(), mission.getDesc(), nextTransfer.getName(), task.getExecutionId(), task.getRootExecutionId(), mission.getId(), task.getAssignee(), task.getAssigneeTime(), currentTime, currentTime));
		
		// 2、插入历史流程表
		Execution e = task.getExecution();
		executionRepository.insertHisExecution(
				new HisExecution(e.getId(), task.getAssignee(), e.getName(), e.getDesc(), e.getBusinessKey(), e.getBusinessDesc(), e.getBusinessType(), e.getDeployId(), new Date(), null, true, task.getRootExecutionId()));
		
	}

}
